package com.chang.rentmall.controllers;

import com.chang.rentmall.forms.AddAddressFormBean;
import com.chang.rentmall.servicesImpl.AddressServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;

@RestController
public class AddressController {
    @Autowired
    AddressServiceImpl addressServiceImpl;

    /**
     * 创建地址
     * @param addAddressFormBean
     * @return
     */
    @RequestMapping(value = "/address/create", method = {RequestMethod.POST})
    public HashMap<String, Object> addAddress (@RequestBody @Valid AddAddressFormBean addAddressFormBean) {
        return addressServiceImpl.addAddress(addAddressFormBean.getForm());
    }

    /**
     * 查询用户的地址
     * @param addAddressFormBean
     * @return
     */
    @RequestMapping(value = "/address/list", method = {RequestMethod.POST})
    public HashMap<String, Object> getAddress (@RequestBody AddAddressFormBean addAddressFormBean) {
        return addressServiceImpl.getAddresses(addAddressFormBean.getQuery().getId());
    }

    /**
     * 更新地址
     * @param addAddressFormBean
     * @return
     */
    @RequestMapping(value = "/address/update", method = {RequestMethod.POST})
    public HashMap<String, Object> updateAddress (@RequestBody AddAddressFormBean addAddressFormBean) {
        return addressServiceImpl.updateAddress(addAddressFormBean.getForm());
    }

    /**
     * 删除地址
     * @param addAddressFormBean
     * @return
     */
    @RequestMapping(value = "/address/delete", method = {RequestMethod.POST})
    public HashMap<String, Object> deleteAddress (@RequestBody AddAddressFormBean addAddressFormBean) {
        return addressServiceImpl.deleteAddress(addAddressFormBean.getForm().getId());
    }

    /**
     * 根据地址id获取地址详情
     * @param addressFormBean
     * @return
     */
    @RequestMapping(value = "/address/detail", method = {RequestMethod.POST})
    public HashMap<String, Object> getAddressDetail (@RequestBody AddAddressFormBean addressFormBean) {
        return addressServiceImpl.getAddressDetail(addressFormBean.getQuery().getId());
    }
}
